package view.common.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

/**
 * @Archivo CustomDispatchAction.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:39:53 PM       moshiro
 */
public abstract class CustomDispatchAction extends DispatchAction {

    public Logger logger;

    //This constructor creates an instance of the Logger
    public CustomDispatchAction() {
        super();
        logger = Logger.getLogger(this.getClass());
    }

    //These methods allows the addition of custom error messages
    public void sendErrorMessage(HttpServletRequest request, String key) {
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
        saveErrors(request, errors);
    }

    public void sendErrorMessage(HttpServletRequest request, String key, boolean send) {
        ActionMessages errors = new ActionMessages();
        if (send == true) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
            saveErrors(request, errors);
        }
    }

    public void sendSuccessMessage(HttpServletRequest request, String key) {
        ActionMessages messages = new ActionMessages();
        //messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));        
        //saveMessages(request, messages);
    }

    public void sendErrorMessage(HttpSession session, String key) {
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
        saveErrors(session, errors);
    }

    public void sendSuccessMessage(HttpSession session, String key) {
        ActionMessages messages = new ActionMessages();
        //messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));        
        //saveMessages(session, messages);
    }

    public void sendSuccessMessage(HttpSession session, String key, boolean send) {
        ActionMessages messages = new ActionMessages();
        if (send == true) {
            messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
            saveMessages(session, messages);
        }
    }

    public void sendErrorMessage(HttpServletRequest request, String key,
            String param) {
        ActionMessages errors = new ActionMessages();
        ActionMessage msg = new ActionMessage(key, param);
        errors.add(ActionMessages.GLOBAL_MESSAGE, msg);
        saveErrors(request, errors);
    }

    public void sendErrorMessage(HttpServletRequest request, String key,
            Object[] params) {
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage(key, params));
        saveErrors(request, errors);
    }

    protected void removeFormBean(ActionMapping mapping, HttpServletRequest request) {
        // Remove the obsolete form bean
        if (mapping.getAttribute() != null) {
            if ("request".equals(mapping.getScope())) {
                request.removeAttribute(mapping.getAttribute());
            } else {
                HttpSession session = request.getSession();
                session.removeAttribute(mapping.getAttribute());
            }
        }
    }
}
